package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.adapter.GuidePagerAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/1/7.
 */
public class GuideActivity extends AppCompatActivity {
    private ViewPager guideViewPager;
    private ArrayList<View> viewList;
    private Button experienceBtn;
    private PagerAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        SharedPreferences sharedPreferences = getSharedPreferences("first_share",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isFirstIn",true);
        editor.commit();


        //初始化数据源
        initData();
        //初始化视图
        initView();
        //为向导界面设置适配器
        setAdapter();
        //设置向导界面的滑动监听和体验按钮的监听
        setListener();
    }

    private void setListener() {
        //向导界面的滑动监听
        setViewPagerListener();
        //体验按钮的监听
        setExperienceListener();
    }

    /**
     * 体验按键的触摸监听，当按钮被触摸的时候，按钮变更为被按下的图片，当触摸结束的时候，按钮变回原图片样式
     */
    private void setExperienceListener() {
      experienceBtn.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              if (v.getId() == R.id.experience_btn){
                  if (event.getAction() == MotionEvent.ACTION_DOWN){
                      experienceBtn.setBackgroundResource(R.mipmap.ic_guide_btn_pressed);
                  }
                  if (event.getAction() == MotionEvent.ACTION_UP){
                      experienceBtn.setBackgroundResource(R.mipmap.ic_guide_btn_normal);
                      //当触摸结束按钮弹起时，进入跳转界面
                      Intent intent = new Intent(GuideActivity.this,MainActivity.class);
                      startActivity(intent);
                      finish();
                  }
              }
              return false;
          }
      });
    }

    /**
     * 向导界面的滑动监听
     */
    private void setViewPagerListener() {
        guideViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //当滑动页面时，变更到相应的页面
                guideViewPager.setCurrentItem(position);
                //当变更为第四个页面的时候，设置体验按钮显示，其他页面设置体验按钮隐藏
                if(position == 3){
                    experienceBtn.setVisibility(View.VISIBLE);
                }else {
                    experienceBtn.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 为向导页面设置适配器
     */
    private void setAdapter() {
        adapter = new GuidePagerAdapter(viewList);
        guideViewPager.setAdapter(adapter);

    }

    /**
     * 初始化视图，并为向导页面设置初始视图布局
     */
    private void initView() {
        guideViewPager = (ViewPager)findViewById(R.id.guide_viewpager);
        guideViewPager.setCurrentItem(0);
        experienceBtn = (Button)findViewById(R.id.experience_btn);
    }

    /**
     * 初始化向导页面数据资源
     */
    private void initData() {
        viewList = new ArrayList<View>();
        LayoutInflater inflater = LayoutInflater.from(this);
        View view1 = inflater.inflate(R.layout.guide1,null);
        View view2 = inflater.inflate(R.layout.guide2,null);
        View view3 = inflater.inflate(R.layout.guide3,null);
        View view4 = inflater.inflate(R.layout.guide4,null);
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);
    }


}
